package top.lixunda.ecommerce.server.good.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.SysGoodCategoryDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodCategoryInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.GoodCategoryUpdateDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.SysGoodCategoryTreeVO;
import top.lixunda.ecommerce.server.good.api.entity.vo.SysGoodCategoryVO;
import top.lixunda.ecommerce.server.good.api.controller.ISysGoodCategoryController;
import top.lixunda.ecommerce.server.good.service.ISysGoodCategoryService;

import java.util.List;

/**
 * 商品分类管理，此商品分类为系统商品分类参数，超级管理员权限配置
 *
 * @author Xumda
 * @version time:2020/1/11 11:12
 */
@Slf4j
@RestController
public class SysGoodCategoryController extends BaseController
        implements ISysGoodCategoryController {

    private final ISysGoodCategoryService sysGoodCategoryService;

    @Autowired
    public SysGoodCategoryController(ISysGoodCategoryService sysGoodCategoryService) {
        this.sysGoodCategoryService = sysGoodCategoryService;
    }

    /**
     * 添加一个商品分类
     *
     * @param goodCategoryInsertDTO 新添加的商品分类数据
     * @return 是否添加成功
     */
    @Override
    public MessageObject<Boolean> insertGoodCategory(GoodCategoryInsertDTO goodCategoryInsertDTO) {
        boolean success = sysGoodCategoryService.insertGoodCategory(goodCategoryInsertDTO);
        return success ? getSuccessMessageObject("添加商品分类数据成功", true) :
                getErrorMessageObject("添加商品分类数据失败", false);
    }

    /**
     * 更新一个商品分类
     *
     * @param goodCategoryUpdateDTO 需要更新的商品分类数据
     * @return 是否更新成功
     */
    @Override
    public MessageObject<Boolean> updateGoodCategory(GoodCategoryUpdateDTO goodCategoryUpdateDTO) {
        boolean success = sysGoodCategoryService.updateGoodCategory(goodCategoryUpdateDTO);
        return success ? getSuccessMessageObject("更新商品分类数据成功", true) :
                getErrorMessageObject("更新商品分类数据失败", false);
    }

    /**
     * 根据id删除商品分类，只删除该节点，其子节点全部放到父节点中去
     *
     * @param id 商品分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodCategoryById(Integer id) {
        boolean success = sysGoodCategoryService.removeGoodCategoryById(id);
        return success ? getSuccessMessageObject("移除商品分类数据成功", true) :
                getErrorMessageObject("移除商品分类数据失败", false);
    }

    /**
     * 根据id删除商品分类，只删除该节点，其子节点全部放到父节点中去
     *
     * @param ids 商品分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodCategoryByIds(List<Integer> ids) {
        boolean success = sysGoodCategoryService.removeGoodCategoryByIds(ids);
        return success ? getSuccessMessageObject("移除商品分类数据成功", true) :
                getErrorMessageObject("移除商品分类数据失败", false);
    }

    /**
     * 根据id删除商品分类，删除包括子节点的所有节点
     *
     * @param id 商品分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodCategoryAllById(Integer id) {
        boolean success = sysGoodCategoryService.removeGoodCategoryAllById(id);
        return success ? getSuccessMessageObject("移除商品分类数据成功", true) :
                getErrorMessageObject("移除商品分类数据失败", false);
    }

    /**
     * 根据id删除商品分类，删除包括子节点的所有节点
     *
     * @param ids 商品分类id
     * @return 是否删除成功
     */
    @Override
    public MessageObject<Boolean> removeGoodCategoryAllByIds(List<Integer> ids) {
        boolean success = sysGoodCategoryService.removeGoodCategoryAllByIds(ids);
        return success ? getSuccessMessageObject("移除商品分类数据成功", true) :
                getErrorMessageObject("移除商品分类数据失败", false);
    }

    /**
     * 根据id获取分类数据
     *
     * @param id 分类id
     * @return 单条分类数据
     */
    @Override
    public MessageObject<SysGoodCategoryVO> queryById(Integer id) {
        SysGoodCategoryDO sysGoodCategoryDO = sysGoodCategoryService.queryById(id);
        SysGoodCategoryVO vo = SysGoodCategoryVO.fromGoodCategoryDO(sysGoodCategoryDO);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 查询整个分类树
     *
     * @return 分类树
     */
    @Override
    public MessageObject<SysGoodCategoryTreeVO> queryTree() {
        List<SysGoodCategoryDO> list = sysGoodCategoryService.queryAll();
        SysGoodCategoryTreeVO vo = SysGoodCategoryTreeVO.fromGoodCategoryDoList(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 根据父节点id获取子类节点列表
     *
     * @param id 父节点id
     * @return 子节点列表
     */
    @Override
    public MessageObject<List<SysGoodCategoryVO>> queryChildrenByParentId(Integer id) {
        List<SysGoodCategoryDO> list = sysGoodCategoryService.queryChildrenByParentId(id);
        List<SysGoodCategoryVO> vo = SysGoodCategoryVO.fromGoodCategoryDO(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }

    /**
     * 查询首层节点
     *
     * @return 首层节点
     */
    @Override
    public MessageObject<List<SysGoodCategoryVO>> queryRoots() {
        List<SysGoodCategoryDO> list = sysGoodCategoryService.queryRoots();
        List<SysGoodCategoryVO> vo = SysGoodCategoryVO.fromGoodCategoryDO(list);
        return getSuccessMessageObject("获取数据成功", vo);
    }
}
